using UnityEngine;
using System.Collections;


public class SATouchTerrain : MonoBehaviour 
{
	private bool useMouse = true;
	private bool useTouch = true;
	private bool isEditor = false;
	
	static public Vector2 lastTouchPosition = Vector2.zero;
	
	static public MouseOrTouch currentTouch = null;
	
	static public int currentTouchID = -1;
	static public bool isTouch = false;

	private Camera touchCamera = null;

	public class MouseOrTouch
	{
		public Vector2 pos;			// Current position of the mouse or touch event
		public Vector2 delta;		// Delta since last update
		public Vector2 totalDelta;	// Delta since the event started being tracked
	}
	
	static MouseOrTouch[] mMouse = new MouseOrTouch[] { new MouseOrTouch(), new MouseOrTouch(), new MouseOrTouch() };
	
	void Start()
	{
		if (Application.platform == RuntimePlatform.Android ||
		    Application.platform == RuntimePlatform.IPhonePlayer)
		{
			useMouse = false;
			useTouch = true;
		}
		else if (Application.platform == RuntimePlatform.PS3 ||
		         Application.platform == RuntimePlatform.XBOX360)
		{
			useMouse = false;
			useTouch = false;
		}
		else if (Application.platform == RuntimePlatform.WindowsEditor ||
		         Application.platform == RuntimePlatform.OSXEditor)
		{
			isEditor = true;
		}

		touchCamera = GameObject.FindWithTag( "MainCamera" ).GetComponent< Camera >();
	}
	
	
	void ProcessMouse ()
	{
		if( UICamera.hoveredObject != null )
		{
			return;
		}
		
		bool updateRaycast = (Time.timeScale < 0.9f);
		
		if (!updateRaycast)
		{
			for (int i = 0; i < 3; ++i)
			{
				if (Input.GetMouseButton(i) || Input.GetMouseButtonUp(i))
				{
					updateRaycast = true;
					break;
				}
			}
		}
		
		// Update the position and delta
		mMouse[0].pos = Input.mousePosition;
		mMouse[0].delta = mMouse[0].pos - lastTouchPosition;
		
		bool posChanged = (mMouse[0].pos != lastTouchPosition);
		lastTouchPosition = mMouse[0].pos;
		
		// Update the object under the mouse
		//if (updateRaycast) mMouse[0].current = Raycast(Input.mousePosition, ref lastHit) ? lastHit.collider.gameObject : fallThrough;
		
		// Propagate the updates to the other mouse buttons
		for (int i = 1; i < 3; ++i)
		{
			mMouse[i].pos = mMouse[0].pos;
			mMouse[i].delta = mMouse[0].delta;
			//mMouse[i].current = mMouse[0].current;
		}
		
		// Is any button currently pressed?
		bool isPressed = false;
		
		for (int i = 0; i < 3; ++i)
		{
			if (Input.GetMouseButton(i))
			{
				isPressed = true;
				break;
			}
		}
		
		
		// Process all 3 mouse buttons as individual touches
		for (int i = 0; i < 3; ++i)
		{
			bool pressed = Input.GetMouseButtonDown(i);
			bool unpressed = Input.GetMouseButtonUp(i);
			
			currentTouch = mMouse[i];
			currentTouchID = -1 - i;
			
			ProcessTouch(pressed, unpressed);
		}
		
		currentTouch = null;
	}
	
	void UpdateBound()
	{

	}


	void foo()
	{
		float x = touchCamera.transform.localPosition.x - Screen.width * 0.5f / GameSetting.activeRes.showSacle + lastTouchPosition.x / GameSetting.activeRes.showSacle;
		float y = touchCamera.transform.localPosition.y - Screen.height * 0.5f / GameSetting.activeRes.showSacle + lastTouchPosition.y / GameSetting.activeRes.showSacle;

		int posX = (int)( x / SAMapGrid.GridX + y / SAMapGrid.GridY + 0.5f );
		int posY = (int)( - y / SAMapGrid.GridY + x / SAMapGrid.GridX + 0.5f );
	
		//Debug.Log( x + " " + y + " " +  posX + " " + posY );

		SAMapManager.instance.findPath( posX , posY );
	}
	
	
	void ProcessTouch ( bool pressed , bool unpressed )
	{
		if ( pressed )
		{
			isTouch = true;
		}
		
		if ( unpressed )
		{
			isTouch = false;

			if ( UICamera.currentTouch == null )
			{
				foo ();
			}
		}
	}
	
	
	void Update()
	{
		if ( useMouse && ( useTouch && isEditor ) ) 
		{
			ProcessMouse();
		}
		else
		{
			if ( Input.touchCount > 0 )
			{
				isTouch = true;			
				
				lastTouchPosition = Input.GetTouch( 0 ).position;
			}
			else
			{
				if ( isTouch )
				{
					isTouch = false;
					
					if ( UICamera.currentTouch == null )
					{
						foo ();
					}
				}
				
			}
		}
		
		
	}
	
}



